package client.boot;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;

import vo.AccountBookVo;
import blservice.accountbook.AccountBookBLService;
import blservice.client.ClientBLService;
import blservice.commodity.CommodityBLService;
import blservice.commodity.CommodityTypeBLService;
import blservice.commodity.StockBLService;
import blservice.document.DocumentBLService;
import blservice.finance.AccountorBLService;
import blservice.history.SaleLogBLService;
import blservice.history.SystemLogBLService;
import blservice.history.经营情况BLService;
import blservice.loader.ServiceLoaderService;
import blservice.strategy.StrategyBLService;
import blservice.user.UserBLService;

public class BLServiceManager {
	private static String serverIP;
	private static final int serverPort=9587;
	private static ServiceLoaderService loader;
	private static AccountBookBLService abbl;
	public static void setServerIP(String ip) throws MalformedURLException, RemoteException, NotBoundException, SQLException{
		serverIP=ip;
		loader=(ServiceLoaderService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/ServiceLoaderService");
		abbl=getAccountBookBLService();
		
	}
	
	public static AccountBookBLService getAccountBookBLService() throws RemoteException, MalformedURLException, NotBoundException, SQLException{
		try {
			return (AccountBookBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/AccountBookBLService");
		} catch (NotBoundException e) {
			try {
				loader.loadAccountBookBLService();
			} catch (AlreadyBoundException e1) {
				e1.printStackTrace();
			}
			return (AccountBookBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/AccountBookBLService");
		}
	}
	
	public static ClientBLService getClientBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, NotBoundException, SQLException{
		if(ab==null)
			try {
				return (ClientBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/ClientBLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.loadClientBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (ClientBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/ClientBLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (ClientBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/ClientBLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.loadClientBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (ClientBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/ClientBLService."+ab.getName());
			}
	}
	
	public static CommodityBLService getCommodityBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, NotBoundException, SQLException{
		if(ab==null)
			try {
				return (CommodityBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/CommodityBLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.loadCommodityBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (CommodityBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/CommodityBLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (CommodityBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/CommodityBLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.loadCommodityBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (CommodityBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/CommodityBLService."+ab.getName());
			}
	}
	
	public static CommodityTypeBLService getCommodityTypeBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, NotBoundException, SQLException{
		if(ab==null)
			try {
				return (CommodityTypeBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/CommodityTypeBLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.loadCommodityTypeBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (CommodityTypeBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/CommodityTypeBLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (CommodityTypeBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/CommodityTypeBLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.loadCommodityTypeBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (CommodityTypeBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/CommodityTypeBLService."+ab.getName());
			}
	}
	
	public static StockBLService getStockBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, NotBoundException, SQLException{
		if(ab==null)
			try {
				return (StockBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/StockBLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.loadStockBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (StockBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/StockBLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (StockBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/StockBLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.loadStockBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (StockBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/StockBLService."+ab.getName());
			}
	}
	
	public static AccountorBLService getAccountorBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, NotBoundException, SQLException{
		if(ab==null)
			try {
				return (AccountorBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/AccountorBLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.loadAccountorBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (AccountorBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/AccountorBLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (AccountorBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/AccountorBLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.loadAccountorBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (AccountorBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/AccountorBLService."+ab.getName());
			}
	}
	
	public static 经营情况BLService get经营情况BLService(AccountBookVo ab) throws RemoteException, MalformedURLException, NotBoundException, SQLException{
		if(ab==null)
			try {
				return (经营情况BLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/经营情况BLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.load经营情况BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (经营情况BLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/经营情况BLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (经营情况BLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/经营情况BLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.load经营情况BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (经营情况BLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/经营情况BLService."+ab.getName());
			}
	}
	
	public static SaleLogBLService getSaleLogBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, NotBoundException, SQLException{
		if(ab==null)
			try {
				return (SaleLogBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/SaleLogBLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.loadSaleLogBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (SaleLogBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/SaleLogBLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (SaleLogBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/SaleLogBLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.loadSaleLogBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (SaleLogBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/SaleLogBLService."+ab.getName());
			}
	}
	
	public static SystemLogBLService getSystemLogBLService() throws RemoteException, MalformedURLException, NotBoundException, SQLException{
		try {
			return (SystemLogBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/SystemLogBLService");
		} catch (NotBoundException e) {
			try {
				loader.loadSystemLogBLService();
			} catch (AlreadyBoundException e1) {
				e1.printStackTrace();
			}
			return (SystemLogBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/SystemLogBLService");
		}
	}
	
	public static StrategyBLService getStrategyBLService(AccountBookVo ab) throws RemoteException, MalformedURLException, NotBoundException, SQLException{
		if(ab==null)
			try {
				return (StrategyBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/StrategyBLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.loadStrategyBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (StrategyBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/StrategyBLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (StrategyBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/StrategyBLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.loadStrategyBLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (StrategyBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/StrategyBLService."+ab.getName());
			}
	}
	
	public static UserBLService getUserBLService() throws RemoteException, MalformedURLException, NotBoundException, SQLException{
		try {
			return (UserBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/UserBLService");
		} catch (NotBoundException e) {
			try {
				loader.loadUserBLService();
			} catch (AlreadyBoundException e1) {
				e1.printStackTrace();
			}
			return (UserBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/UserBLService");
		}
	}
	
	public static DocumentBLService load进退货单BLService(AccountBookVo ab)throws RemoteException, MalformedURLException, SQLException, NotBoundException{
		if(ab==null)
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/进退货单BLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.load进退货单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/进退货单BLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/进退货单BLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.load进退货单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/进退货单BLService."+ab.getName());
			}
	}
	
	public static DocumentBLService load库存报警单BLService(AccountBookVo ab)throws RemoteException, MalformedURLException, SQLException, NotBoundException{
		if(ab==null)
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存报警单BLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.load库存报警单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存报警单BLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存报警单BLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.load库存报警单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存报警单BLService."+ab.getName());
			}
	}
	
	public static DocumentBLService load库存报溢报损单BLService(AccountBookVo ab)throws RemoteException, MalformedURLException, SQLException, NotBoundException{
		if(ab==null)
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存报溢报损单BLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.load库存报溢报损单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存报溢报损单BLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存报溢报损单BLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.load库存报溢报损单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存报溢报损单BLService."+ab.getName());
			}
	}
	
	public static DocumentBLService load库存赠送单BLService(AccountBookVo ab)throws RemoteException, MalformedURLException, SQLException, NotBoundException{
		if(ab==null)
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存赠送单BLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.load库存赠送单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存赠送单BLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存赠送单BLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.load库存赠送单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/库存赠送单BLService."+ab.getName());
			}
	}
	
	public static DocumentBLService load收付款单BLService(AccountBookVo ab)throws RemoteException, MalformedURLException, SQLException, NotBoundException{
		if(ab==null)
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/收付款单BLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.load收付款单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/收付款单BLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/收付款单BLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.load收付款单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/收付款单BLService."+ab.getName());
			}
	}
	
	public static DocumentBLService load现金费用单BLService(AccountBookVo ab)throws RemoteException, MalformedURLException, SQLException, NotBoundException{
		if(ab==null)
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/现金费用单BLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.load现金费用单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/现金费用单BLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/现金费用单BLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.load现金费用单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/现金费用单BLService."+ab.getName());
			}
	}
	
	public static DocumentBLService load销退售单BLService(AccountBookVo ab)throws RemoteException, MalformedURLException, SQLException, NotBoundException{
		if(ab==null)
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/销退售单BLService."+abbl.getDefaultAccountBook().getName());
			} catch (NotBoundException e) {
				try {
					loader.load销退售单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/销退售单BLService."+abbl.getDefaultAccountBook().getName());
			}
		else
			try {
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/销退售单BLService."+ab.getName());
			} catch (NotBoundException e) {
				try {
					loader.load销退售单BLService(ab);
				} catch (AlreadyBoundException e1) {
					e1.printStackTrace();
				}
				return (DocumentBLService) Naming.lookup("rmi://"+serverIP+":"+serverPort+"/销退售单BLService."+ab.getName());
			}
	}
}
